﻿using RightControl.Common;
using RightControl.IService;
using RightControl.Model;
using RightControl.WebApp.Areas.Admin.Controllers;
using System;
using System.Web.Mvc;
using AlibabaCollector;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;

namespace RightControl.WebApp.Areas.DataSrv.Controllers {
    public class FinishController : BaseController {
        public static IExportFieldsService exportFieldsService { get; set; }
        public static IBaseCategoryService baseCategoryService { get; set; }
        public static ITranslationService translationService { get; set; }
        public static IProductService productService { get; set; }
        public static ISubProductImageService subProductImgService { get; set; }
        public static ISubProductSizeService subProductSizeService { get; set; }
        public static IAmazonService amazonService { get; set; }
        public static IDhGateService dhgateService { get; set; }
        public static IFieldMapService fieldMapSvr { get; set; }
        public static IProductImgService productImgService { get; set; }
        public static IPlatformAuthService platformAuthSvr { get; set; }

        public FinishController() {          
           
        }

        public  ActionResult Index(int? id) {          

            var exportFields = exportFieldsService.GetExportFields("NZ", 1);
            ViewData["TableHead"] = exportFields;
           // ViewData["OptionsData_En"] = SelectOptionsDataEnglish(exportFields);
            ViewData["BaseCategory"] = baseCategoryService.GetAllCategory();
            base.Initialize(id);
            return View();
        }


        public static Dictionary<string, string> SelectOptionsDataEnglish(List<ExportFields> fieldsList = null) {
            return CacheHelper.GetCache<Dictionary<string, string>>("selItems", () => {
                Dictionary<string, string> selectItemDic = new Dictionary<string, string>();
                if (fieldsList != null && fieldsList.Count > 0) {
                    string wordKey = "", optionKey = "";
                    List<string> trans;
                    foreach (var item in fieldsList) {
                        trans = new List<string>();
                        if (!string.IsNullOrEmpty(item.Candidates)) {
                            HashSet<string> words = new HashSet<string>(item.Candidates.Split('^'));

                            //foreach (var candidate in item.Candidates.Split('^')) {
                            //    if (!string.IsNullOrEmpty(candidate)) {
                            //        words.Add(string.Format("'{0}'", candidate));
                            //    }
                            //}

                            Dictionary<string, Translation> translateDic = translationService.GetWordsTranslation(words);

                            foreach (var word in words) {
                                wordKey = word.Trim("'".ToCharArray());
                                trans.Add(translateDic.ContainsKey(wordKey) ? translateDic[wordKey].TransEnglish : wordKey);
                            }
                            optionKey = item.EngFieldNm;
                            selectItemDic[optionKey] = string.Join("^", trans);
                        }
                    }
                }
                return selectItemDic;
            }, 600);
        }


        [HttpPost]
        public ActionResult AddAmazonMeta(int productId) {
            var productDetail = productService.ReadModel(productId);
            int ret = amazonService.AddAmazonMeta(productDetail, Operator.UserId);

            var result = ret > 0 ? SuccessTip() : ErrorTip();
            return Json(result);
        }

        [HttpPost]
        public ActionResult AddDhGateMeta(int productId) {
            var productDetail = productService.ReadModel(productId);
            int ret = dhgateService.AddDhGateMeta(productDetail, Operator.UserId);        

            var result = ret > 0 ? SuccessTip() : ErrorTip();
            return Json(result);
        }
    }
}